package com.feiyisys.config;

import com.feiyisys.interceptors.AdminAuthInterceptor;
import com.feiyisys.interceptors.AuthorizationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Resource
    private AuthorizationInterceptor authorizationInterceptor;
    
    @Resource
    private AdminAuthInterceptor adminAuthInterceptor;

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    private final List<String> whitelist = List.of(
            "/",
            "/user/login",
            "/admin/login",
            "/doc.html",
            "/webjars/**",
            "/v3/api-docs/**",
            "/home",
            "/index/**",
            "/stream",
            "/user/register",
            "/user/findPwd",
            "/activity/upload"
    );


    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("{}", whitelist);
        
        // 注册基础认证拦截器
        registry.addInterceptor(authorizationInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(whitelist.toArray(new String[0])); // 排除白名单路径
        
        // 注册管理员权限验证拦截器
        registry.addInterceptor(adminAuthInterceptor)
                .addPathPatterns("/feedback/reply", "/feedback/status/**", "/feedback/all");
    }


}
